/**
 * 应用输入窗口
 */
Ext.namespace("Ext.ux.org.omorg");

Ext.ux.org.omorg.OrgNodeDragDropWindow = Ext.extend(Ext.Window, {
	
	title: '选择操作',
	layout: 'fit',
	width: 250,
	height: 155,
	border: false,
	plain: true,
	modal: true,
	
	dropNode: '',	//被拖动节点
	targetNode: '',	//被拖动节点指向的目标节点

	formPanel: '',

	initComponent: function(){
		Ext.ux.org.omorg.OrgNodeDragDropWindow.superclass.initComponent.call(this);
		
		var operSelection = function(button, e){
			// 移动 或 复制 操作
			var operation = detailForm.findById(this.id + '_operationRadio').getValue().inputValue
			
			// 被拖动节点类型
			var dropPartyType = this.dropNode.attributes.partyType;
			// 目标节点类型
			var targetPartyType = this.targetNode.attributes.partyType;

			if(operation === 'move'){
				// 移动机构
				if(dropPartyType === 'ORG' && targetPartyType === 'ORG'){
					Ext.Ajax.request({
						url: 'om-org!moveOrg.action',   
						success: function (result, request){
							this.refreshNode();
							this.close();
						},    
						failure: function (result, request){
							this.close();
							Ext.Msg.alert('系统提示', '移动机构操作失败！');
						},    
						method: 'POST',    
						params:{
							orgId: this.dropNode.attributes.orgId,
							parentOrgId: this.targetNode.attributes.orgId
						},
						scope: this
					});
				}
				// 移动用户至机构
				else if(dropPartyType === 'USER' && targetPartyType === 'ORG'){
					Ext.Ajax.request({
						url: 'om-org!moveUserToOrg.action',   
						success: function (result, request){
							this.refreshNode();
							this.close();
						},    
						failure: function (result, request){
							this.close();
							Ext.Msg.alert('系统提示', '移动用户至机构操作失败！');
						},    
						method: 'POST',    
						params:{
							userId: this.dropNode.attributes.userId,
							oldOrgId: this.dropNode.attributes.orgId,
							targetOrgId: this.targetNode.attributes.orgId
						},
						scope: this
					});
				}
				// 移动用户至岗位
				else if(dropPartyType === 'USER' && targetPartyType === 'POSITION'){
					Ext.Ajax.request({
						url: 'om-org!moveUserToPosition.action',   
						success: function (result, request){
							this.refreshNode();
							this.close();
						},    
						failure: function (result, request){
							this.close();
							Ext.Msg.alert('系统提示', '移动用户至岗位操作失败！');
						},    
						method: 'POST',    
						params:{
							userId: this.dropNode.attributes.userId,
							oldPosId: this.dropNode.attributes.posId,
							targetPosId: this.targetNode.attributes.posId
						},
						scope: this
					});
				}
			}else if(operation === 'copy'){
				alert(this.dropNode.attributes.partyType);
				alert(this.dropNode.attributes.userId);
				alert(this.dropNode.attributes.orgId);
				alert(this.targetNode.attributes.orgId);
			
				Ext.Ajax.request({
					url: 'om-org!moveUserToOrg.action',   
					success: function (result, request){    
						delMask.hide();
						var appTree = Ext.getCmp('appTree');
						appTree.fireEvent("refresh",appTree.root);
						
						var appQueryPanel = Ext.getCmp('appQueryPanel');
						if(appQueryPanel){
							appQueryPanel.gridPanel.getStore().load();
						}
						
					},    
					failure: function (result, request){    
						delMask.hide();
						Ext.Msg.alert('系统提示', '删除操作失败！');
					},    
					method: 'POST',    
					params:{
						userId: this.dropNode.attributes.userId,
						oldOrgId: this.dropNode.attributes.orgId,
						targetOrgId: this.targetNode.attributes.orgId
					}    
				});
			}
		}
		
		//条件查询面板	
		var detailForm = new Ext.FormPanel({
			frame: true,
			plain: true,
			labelWidth: 10,
			items: [{
				xtype:'fieldset',
	            title: '<span class="fs_tip">请选择要执行的操作</span>',
	            layout: 'form',
				items:[{
					xtype: 'radiogroup',
					id : this.id + '_operationRadio', 
		            items: [
		                {boxLabel:'移动', inputValue:'move', checked: true},
		                {boxLabel:'复制', inputValue:'copy', disabled: true}
		            ]
				}]
			}],
			buttons: [{
				text: '&nbsp;确定',
				width: 70,
				iconCls: 'icon-middle-ok',
				scale: 'medium',
				handler: operSelection,
				scope: this
			},{
				text: '&nbsp;取消',
				width: 70,
				iconCls: 'icon-middle-cancel',
				scale: 'medium',
				handler: function(){
					this.close();
				},
				scope: this
			}]
	    });
	    this.formPanel = detailForm;
		this.add(detailForm);
	},
	
	refreshNode: function(){
		var orgTree = Ext.getCmp('orgTree');
		
		var targetNode = orgTree.getNodeById(this.targetNode.id);
		if(targetNode != null && targetNode.attributes.partyType != 'USER')
			targetNode.reload();

		if(this.dropNode.parentNode != null && this.dropNode.parentNode.id != null){
			var parentNode = orgTree.getNodeById(this.dropNode.parentNode.id);
			if(parentNode != null)
				parentNode.reload();
		}
	}
});